import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/config/authConfig'

// 路由懒加载
const Layout = () => import(/* webpackChunkName: "Welcome" */ '../views/layout/Layout.vue')
const Home = () => import(/* webpackChunkName: "Welcome" */ '../views/h5/Home.vue')
const About = () => import(/* webpackChunkName: "Welcome" */ '../views/h5/About.vue')
const Project = () => import(/* webpackChunkName: "Welcome" */ '../views/h5/Project.vue')
const Message = () => import(/* webpackChunkName: "Welcome" */ '../views/h5/Message.vue')
const Essay = () => import(/* webpackChunkName: "Welcome" */ '../views/h5/Essay.vue')

const Blog = () => import(/* webpackChunkName: "Blog" */ '../views/h5/Blog.vue')

const AdminLayout = () => import(/* webpackChunkName: "Home" */ '../views/layout/AdminLayout.vue')
const AdminIndex = () => import(/* webpackChunkName: "Home" */ '../views/admin/AdminIndex.vue')

const Blogs = () => import(/* webpackChunkName: "Blog_Home" */ '../views/admin/BlogManage.vue')
const BlogInput = () => import(/* webpackChunkName: "Blog_Home" */ '../views/admin/AEditBlog.vue')

const Tags = () => import(/* webpackChunkName: "Tags" */ '../views/admin/TagManage.vue')
const Types = () => import(/* webpackChunkName: "Types" */ '../views/admin/TypeManage.vue')

const Essays = () => import(/* webpackChunkName: "Essay" */ '../views/admin/EssayManage.vue')

const Projects = () => import(/* webpackChunkName: "Projects" */ '../views/admin/ProjectManage.vue')

const Comments = () => import(/* webpackChunkName: "Comment" */ '../views/admin/CommentManage.vue')

const Administrator = () => import(/* webpackChunkName: "Admin" */ '../views/admin/Administrator.vue')
const Users = () => import(/* webpackChunkName: "User" */ '../views/admin/UserManage.vue')

const Error = () => import(/* webpackChunkName: "Error" */ '../views/h5/Error.vue')
const Pictures = () => import(/* webpackChunkName: "Pictures" */ '../views/admin/PictureManage.vue')


Vue.use(VueRouter)

const routes = [
    {
        path: '/error',
        component: Error
    },
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        children: [
            { path: '/home', component: Home },
            { path: '/about', component: About },
            { path: '/blogInfo', component: Blog },
            { path: '/project', component: Project },
            { path: '/message', component: Message },
            { path: '/essay', component: Essay },
        ]
    },
    {
        path: '/admin',
        component: AdminLayout,
        // 挂载路由导航守卫
        beforeEnter: (to, from, next) => {
            if (getToken()) {
                next()
            } else {
                next({
                    path: '/',
                    replace: true
                })
            }
        },
        redirect: '/admin/index',
        children: [
            { path: '/admin/index', component: AdminIndex },
            { path: '/admin/blogs', component: Blogs },
            {
                path: '/admin/blog/add', name: 'AddBlog',
                component: () => import("../views/admin/AEditBlog.vue"),
                meta: { title: '新增项目' }
            },
            {
                path: '/admin/blog/edit', name: 'EditBlog',
                component: () => import("../views/admin/AEditBlog.vue"),
                meta: { title: '新增项目' }
            },
            { path: '/admin/administrator', component: Administrator },
            { path: '/admin/types', component: Types },
            { path: '/admin/tags', component: Tags },
            { path: '/admin/comments', component: Comments },
            { path: '/admin/users', component: Users },
            { path: '/admin/pictures', component: Pictures },
            {
                path: '/admin/message',
                component: () => import("../views/admin/MessageManage.vue"),
                meta: { title: '留言管理' }
            },
            { path: '/admin/projects', component: Projects },
            {
                path: '/admin/project/add',
                name: 'AddProject',
                component: () => import("../views/admin/AEditProject.vue"),
                meta: { title: '新增项目' }
            },
            {
                path: '/admin/project/edit',
                name: 'EditProject',
                component: () => import("../views/admin/AEditProject.vue"),
                meta: { title: '编辑项目' }
            },
            { path: '/admin/essays', component: Essays },
            {
                path: '/admin/essay/add',
                name: 'AddEssay',
                component: () => import("../views/admin/AEditEssay.vue"),
                meta: { title: '新增随笔' }
            },
            {
                path: '/admin/essay/edit',
                name: 'EditEssay',
                component: () => import("../views/admin/AEditEssay.vue"),
                meta: { title: '编辑随笔' }
            },
        ]
    }
]

const router = new VueRouter({
    mode: 'hash',
    routes
})


export default router
